import {
    getOvertimelist,
    getBusinesslist,
    getOutsidelist,
    getTypeList,
    attendOutsideExport,
    attendBusinessExport
} from '@/services/oborecord';
import { attendExport } from '@/services/attendance/overtime';

export default {
    namespace: 'overbusinessrecords',
    state: {
        overtimeList: [],
        overtimePagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        otOrder: '',
        businessList: [],
        businessPagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        bsOrder: '',
        outsideList: [],
        outsidePagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        osOrder: '',
        typeList: [],
        Isovertime: [],
        overType: [],
        overTime: []
    },
    effects: {
        *fetchOvertimeList({ payload }, { call, put }) {
            const result = yield call(getOvertimelist, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        overtimeList: result.data.rows,
                        overtimePagination: {
                            pageSize: result.data.pageSize,
                            current: result.data.pageNo,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        *fetchBusinessList({ payload }, { call, put }) {
            const result = yield call(getBusinesslist, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        businessList: result.data.rows,
                        businessPagination: {
                            pageSize: result.data.pageSize,
                            current: result.data.pageNo,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        *fetchOutsideList({ payload }, { call, put }) {
            const result = yield call(getOutsidelist, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        outsideList: result.data.rows,
                        outsidePagination: {
                            pageSize: result.data.pageSize,
                            current: result.data.pageNo,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        *fetchTypeList({ payload }, { call, put }) {
            const result = yield call(getTypeList, payload);
            const { data } = result;
            if (result && result.code === 10000) {
                const newData = [];
                data.forEach(item => {
                    newData.push({
                        value: item.dictValue,
                        text: item.dictName
                    });
                });
                yield put({
                    type: 'save',
                    payload: {
                        typeList: newData
                    }
                });
            }
        },
        *fetchIsovertimeList({ payload }, { call, put }) {
            const result = yield call(getTypeList, payload);
            const { data } = result;
            if (result && result.code === 10000) {
                const newData = [];
                data.forEach(item => {
                    newData.push({
                        value: item.dictValue,
                        text: item.dictName
                    });
                });
                yield put({
                    type: 'save',
                    payload: {
                        Isovertime: newData
                    }
                });
            }
        },
        // 外出记录导出
        *fetchOutsideExport({ payload }, { call }) {
            const res = yield call(attendOutsideExport, payload);
            return res;
        },
        // 出差记录导出
        *fetchBusinessExport({ payload }, { call }) {
            const res = yield call(attendBusinessExport, payload);
            return res;
        },
        // 加班记录导出
        *fetchOverTimeExport({ payload }, { call }) {
            const res = yield call(attendExport, payload);
            return res;
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
